package operation.modules.operation.controller;


import freemarker.template.TemplateException;
import operation.android.util.StringUtil;
import operation.freeMarker.util.FreemarkerUtil;
import operation.util.ReqResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用Controller
 */
@RestController
@RequestMapping("api/base")
public class BaseController {
    // 文件生成路径
    private static String toFixture = "C:\\Users\\86132\\Desktop\\svn\\OneFixture\\2.src\\trunk\\1.Web\\Server\\fixture-server\\fixture-base-server\\src\\main\\java\\fixture\\base\\modules\\fixture\\controller";
    private static String toAbandonMe = "D:\\workspace\\abandon-me\\server\\src\\main\\java\\operation\\modules\\operation\\controller";

    /**
     * 去生成后端Controller Service ServiceImpl Dao.java xml(sqlServer\mysql)
     * @path: 控制层全路径
     * @ClassName:  类名
     * @EntityName: 实体
     * @Chinese: 中文注释
     * @return
     */
    @RequestMapping("/make-back-end-folder")
    public ReqResult makeBackEndFolder (@RequestParam Map<String, Object> params) {
        return ReqResult.ok().put("success", makeFolder(params));
    }

    public static boolean makeFolder(Map<String, Object> params) {
        String path = params.get("path").toString();
        String[] pathArr = path.split("java");
        String toJava = pathArr[0];
        String toC = pathArr[1].replace("\\controller", "").substring(1);
        String toM = toC.split("\\\\")[toC.split("\\\\").length - 1];
        System.out.println("toJava: --->" + toJava);
        System.out.println("toC: --->" + toC);
        System.out.println("toM: --->" + toM);

        Map<String, Object> map = new HashMap<>();
        map.put("toJava", toJava);
        map.put("toC", toC);
        map.put("toM",toM);
        map.put("toCDot", toC.replace("\\", "."));
        map.put("toMDot",toM.replace("\\", "."));
        map.put("ClassName", params.get("ClassName")); // 类名
        map.put("className", StringUtil.Hump(params.get("ClassName").toString()));
        map.put("classname", StringUtil.BigHumpToOne(params.get("ClassName").toString()));
        map.put("EntityName", params.get("EntityName")); // 实体名称
        map.put("Chinese", params.get("Chinese")); // 中文注释
        try {
            // 生成controller
            FreemarkerUtil.initConfig(2, "Controller模板.ftl");
            FreemarkerUtil.generator(toJava + "java\\" + toC + "\\controller\\" + map.get("ClassName") + "Controller.java", map);
            // 生成service
            FreemarkerUtil.initConfig(2, "Service模板.ftl");
            FreemarkerUtil.generator(toJava + "java\\" + toC + "\\service\\" + map.get("ClassName") + "Service.java", map);
            // 生成serviceImpl
            FreemarkerUtil.initConfig(2, "ServiceImpl模板.ftl");
            FreemarkerUtil.generator(toJava + "java\\" + toC + "\\service\\impl\\" + map.get("ClassName") + "ServiceImpl.java", map);
            // 生成dao.java
            FreemarkerUtil.initConfig(2, "Dao模板.ftl");
            FreemarkerUtil.generator(toJava + "java\\" + toC + "\\dao\\" + map.get("ClassName") + "Dao.java", map);
            // 生成mysql mapper/生成sqlServer mapper
            FreemarkerUtil.initConfig(2, "mapper模板.ftl");
            FreemarkerUtil.generator(toJava + "resources\\mapper\\mysql\\" + toM + "\\" + map.get("ClassName") + "Dao.xml", map);
            FreemarkerUtil.generator(toJava + "resources\\mapper\\sqlserver\\" + toM + "\\" + map.get("ClassName") + "Dao.xml", map);

        } catch (IOException e) {
            System.out.println("读取模板错误");
            return false;
        } catch (TemplateException e) {
            System.out.println("生成模板错误");
            return false;
        }
        return true;
    }

    public static void main(String args[]) {
        Map<String, Object> map = new HashMap<>();
        map.put("path", toAbandonMe);
        map.put("ClassName", "TestAction");
        map.put("Chinese", "测试功能");
        map.put("EntityName", "TestEntity");
        makeFolder(map);
    }

}
